package com.qiantu.api.db;

import android.content.Context;
import android.text.TextUtils;

import com.qiantu.api.R;
import com.qiantu.api.db.database.LocalDataBase;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.db.greendao.RoomBeanDao;

import java.util.List;

/**
 * Created by cjc on 2019/5/7
 */
public class LLRoomDBManager {

    private final String TAG = "LLRoomDBManager";
    private volatile static LLRoomDBManager instance = null;
    private final static Object object = new Object();
    private final RoomBeanDao mRoomBeanDao;
    private Context context;

    private LLRoomDBManager(Context context) {
        this.context = context;
        mRoomBeanDao = LocalDataBase.getInstance().getDaoSession(context).getRoomBeanDao();
    }

    public static LLRoomDBManager getInstance(Context context) {
        if (instance == null) {
            synchronized (object) {
                if (instance == null) {
                    instance = new LLRoomDBManager(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    public void clearFromDB() {
        mRoomBeanDao.deleteAll();
    }

    /**
     * 从服务器更新房间数据
     *
     * @param roomList
     */
    public void putInTx(List<RoomBean> roomList, HandleListener listener) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            for (RoomBean room : roomList) {
                put(room);
            }
            if(listener != null){
                listener.onCompleted();
            }
        });
    }

    /**
     * 从服务器更新房间数据
     *
     * @param roomList
     */
    public void refreshAllRoomsTx(List<RoomBean> roomList, HandleListener listener) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            clearFromDB();
            if(roomList == null){
                return;
            }
            for (RoomBean room : roomList) {
                mRoomBeanDao.save(room);
            }
        });
    }

    /**
     * 保存或者更新房间
     *
     * @param room
     */
    public void put(RoomBean room) {
        if (room == null)
            return;
        String roomSerialNo = room.getRoomSerialNo();
        if (roomSerialNo != null && !TextUtils.isEmpty(roomSerialNo)) {
            RoomBean oldRoom = mRoomBeanDao.queryBuilder()
                    .where(RoomBeanDao.Properties.RoomSerialNo.eq(roomSerialNo))
                    .unique();
            if (oldRoom != null) {
                room.setId(oldRoom.getId());
            }
        }
        mRoomBeanDao.save(room);
    }

    public void editRoomInfo(String roomSerialNo, String roomName, String floorSerialNo, String roomType){
        RoomBean oldRoom = mRoomBeanDao.queryBuilder()
                .where(RoomBeanDao.Properties.RoomSerialNo.eq(roomSerialNo))
                .unique();
        if (oldRoom != null) {
            oldRoom.setRoomName(roomName);
            oldRoom.setFloorSerialNo(floorSerialNo);
            oldRoom.setRoomTypeId(roomType);
            mRoomBeanDao.save(oldRoom);
        }
    }

    /**
     * 通过roomSerialNo获取房间
     *
     * @param roomSerialNo
     * @return
     */
    public RoomBean getRoomByRoomSerialNo(String roomSerialNo) {
        return mRoomBeanDao.queryBuilder().where(RoomBeanDao.Properties.RoomSerialNo.eq(roomSerialNo)).unique();
    }

    /**
     * 通过roomSerialNo获取房间
     *
     * @param roomSerialNo
     * @return
     */
    public RoomBean getRoomBySerialNo(String roomSerialNo) {
        return mRoomBeanDao.queryBuilder().where(RoomBeanDao.Properties.RoomSerialNo.eq(roomSerialNo)).unique();
    }

    public String getFloorRoomName(String serialNo){
        if(TextUtils.isEmpty(serialNo)){
            return "";
        }
        if(RoomBean.ROOM_COMMON_SERIALNO.equals(serialNo)){
            return context.getResources().getString(R.string.common);
        }else{
            RoomBean roomBean = mRoomBeanDao.queryBuilder().where(RoomBeanDao.Properties.RoomSerialNo.eq(serialNo)).unique();
            if(roomBean == null){
                return "";
            }
            FloorBean floor = LLFloorDBManager.getInstance(context).getFloorByFloorSerialNo(roomBean.getFloorSerialNo());
            //空楼层 默认楼层
            if(floor == null || floor.getType() == 1 || TextUtils.isEmpty(floor.getFloorName())){
                return roomBean.getRoomName();
            }else{
                return floor.getFloorName() + roomBean.getRoomName();
            }
        }
    }


    /**
     * 删除房间
     *
     * @param room
     */
    public void remove(RoomBean room) {
        if (room == null)
            return;
        RoomBean oldDevice = mRoomBeanDao.queryBuilder()
                .where(RoomBeanDao.Properties.RoomSerialNo.eq(room.getRoomSerialNo()))
                .unique();
        if (oldDevice != null) {
            mRoomBeanDao.delete(oldDevice);
        }
    }

    /**
     * 删除房间
     *
     * @param roomSerialNo
     */
    public void remove(String roomSerialNo) {
        if (TextUtils.isEmpty(roomSerialNo))
            return;
        RoomBean tbRoom = mRoomBeanDao.queryBuilder()
                .where(RoomBeanDao.Properties.RoomSerialNo.eq(roomSerialNo))
                .unique();
        if (tbRoom != null) {
            mRoomBeanDao.delete(tbRoom);
        }
    }


    /**
     * 获得本地所有房间列表
     *
     * @return
     */
    public List<RoomBean> getAll() {
        return mRoomBeanDao.queryBuilder()
                .orderAsc(RoomBeanDao.Properties.CreateTime)
                .list();
    }

    /**
     * 获得房屋下所有房间
     *
     * @return
     */
    public List<RoomBean> getRoomByHouseSerialNo(String houseSerialNo) {
        return mRoomBeanDao.queryBuilder()
                .where(RoomBeanDao.Properties.HouseSerialNo.eq(houseSerialNo))
                .orderAsc(RoomBeanDao.Properties.CreateTime)
                .list();
    }

    /**
     * 获得楼层下所有房间
     *
     * @return
     */
    public List<RoomBean> getRoomByFloorSerialNo(String floorSerialNo) {
        return mRoomBeanDao.queryBuilder()
                .where(RoomBeanDao.Properties.FloorSerialNo.eq(floorSerialNo))
                .orderAsc(RoomBeanDao.Properties.Sort,RoomBeanDao.Properties.CreateTime,RoomBeanDao.Properties.RoomSerialNo)
                .list();
    }

    public List<RoomBean> getRoomByRoomType(String roomTypeId) {
        return mRoomBeanDao.queryBuilder()
                .where(RoomBeanDao.Properties.RoomTypeId.eq(roomTypeId))
                .orderAsc(RoomBeanDao.Properties.Sort,RoomBeanDao.Properties.CreateTime,RoomBeanDao.Properties.RoomSerialNo)
                .list();
    }

    /**
     * 根据floorSerialNo获得本地所有房间列表
     *
     * @return
     */
    public List<RoomBean> getRoomListByFloorSerialNo(String floorSerialNo) {
        return mRoomBeanDao.queryBuilder()
                .where(RoomBeanDao.Properties.FloorSerialNo.eq(floorSerialNo))
                .orderAsc(RoomBeanDao.Properties.Sort,RoomBeanDao.Properties.CreateTime)
                .list();
    }

    /**
     * 获得房间信息
     *
     * @return
     */
    public RoomBean get(String roomSerialNo) {
        return mRoomBeanDao.queryBuilder()
                .where(RoomBeanDao.Properties.RoomSerialNo.eq(roomSerialNo))
                .unique();
    }

    public void setAllRoomFloorSerialNo(String floorSerialNo, HandleListener listener) {
        List<RoomBean> allRooms = getAll();
        if (allRooms==null || allRooms.isEmpty()) return;
        for (RoomBean roomBean : allRooms){
            roomBean.setFloorSerialNo(floorSerialNo);
        }
        putInTx(allRooms, listener);
    }
}
